import java.util.Iterator;
/**
 * The Class representing the KiPod media player.
 */
public class KiPod{	
	private AVLTree playlists;
	private AVLTree songs;
	private StringComparator stComp;
	/**
	 * Instantiates a new kiPod.
	 */
	public KiPod(){
		stComp=new StringComparator();
		this.playlists=new AVLTree(stComp);
		this.songs=new AVLTree(stComp);
	}

	/**
	 * Adds the song to the player.
	 *
	 * @param song the song
	 */
	public void addSong(Song song){
		this.songs.add(song.getTitle(),song);
		
	}
	
	/**
	 * Adds the playlist to the player.
	 *
	 * @param playlist the playlist
	 */
	public void addPlaylist(Playlist playlist){
		this.playlists.add(playlist.getName(),playlist);
	}
	
	/**
	 * Adds the song to playlist. assumes that {@link #addSong(Song)} was called for the song.
	 *
	 * @param song the song
	 * @param playlist the playlist
	 */
	public void addSongToPlaylist(Song song, Playlist playlist){
		playlist.add(song);
	}

	/**
	 * Removes the playlist.
	 *
	 * @param playlist the playlist
	 */
	public void removePlaylist(Playlist playlist){
		LinkedList toRemFrom=playlist.getKthtillHthSongs(1, playlist.size());//creates a linked list of all the playlist's songs
		Iterator it=toRemFrom.iterator();
		Song currSong;
		while(it.hasNext()){
			currSong=(Song)it.next();
			currSong.remove(playlist);
		}
		this.playlists.remove(playlist.getName());//removes the playlist from the kipod itself
	}

	/**
	 * Removes the song from playlist.
	 *
	 * @param song the song
	 * @param playlist the playlist
	 */
	public void removeSongFromPlaylist(Song song, Playlist playlist){
		playlist.remove(song);//removes the song from the playlist
		song.remove(playlist);//removes the playlist from the song's playlists
	}

	/**
	 * Removes the song.
	 *
	 * @param song the song
	 */
	public void removeSong(Song song){
		this.songs.remove(song.getTitle());//removes the song from the kipod itself
		LinkedList toRemFrom=song.getPlaylists();
		Iterator it=toRemFrom.iterator();
		while(it.hasNext()){
			Playlist pl=(Playlist)it.next();
			pl.remove(song);//removes the song from the playlist//
		}	
	}
	
	/**
	 * Find kth song in playlist.
	 *
	 * @param playlist the playlist
	 * @param k the k
	 * @return the song
	 */
	public Song findKthSongInPlaylist(Playlist playlist,int k){
		return (Song)playlist.findKthSong(k);
	}

	/**
	 * Gets the kth till hth song in playlist.
	 *
	 * @param playlist the playlist
	 * @param k the k
	 * @param h the h
	 * @return the kth till hth song in playlist
	 */
	public LinkedList getKthTillHthSongInPlaylist(Playlist playlist,int k,int h){
		return (LinkedList)playlist.getKthtillHthSongs(k, h);
	}
	
	/**
	 * Find song.
	 *
	 * @param name the name
	 * @return the song
	 */
	public Song findSong(String name){
		return (Song)this.songs.find(name);	
	}

	/**
	 * Find a playlist by a name.
	 *
	 * @param name the name
	 * @return the playlist
	 */
	public Playlist findPlaylist(String name){
		return (Playlist)this.playlists.find(name);
	}
	/**
	 * Num of songs.
	 *
	 * @return the int
	 */
	public int numOfSongs(){
		return this.songs.size();
	}
	
	/**
	 * Num of playlists.
	 *
	 * @return the int
	 */
	public int numOfPlaylists(){
		return this.playlists.size();
	}
}
